<template>
  <div class="padding_10">
    <div v-if="flag == 0">
      <div style="display: flex">
        <div>
          <el-input v-model="goods_name" placeholder="搜索商品名称" />
        </div>
        <div style="margin-left: 10px">
          <el-button type="primary" @click="search">搜索</el-button>
        </div>
      </div>
      <div style="margin-top: 10px">
        <el-table :data="tableData" :empty-text="emptyText" style="width: 100%">
          <el-table-column label="采购单编码" prop="in_sn" />
          <el-table-column label="入库时间" prop="updated_at" />
          <el-table-column label="备注" prop="remark" />
          <el-table-column label="经手人" prop="user_name" />
          <el-table-column label="操作">
            <template slot-scope="scoped">
              <el-button
                v-permissions="{ permission: ['view:SheetIn'] }"
                type="text"
                @click="details(scoped.row)"
              >
                详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-if="total >= 10"
          background
          layout="prev, pager, next"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <div v-if="flag == 1">
      <SheetInDetails :id="id" @showMain="showMain" />
    </div>
  </div>
</template>

<script>
  import SheetInDetails from './components/sheetInDetails.vue'
  import { goodsInList } from '@/api/commodity'

  export default {
    components: {
      SheetInDetails,
    },

    data() {
      return {
        flag: 0,
        pagesize: 10,
        page: 1,
        emptyText: ' ',
        tableData: [],
        goods_name: '',
        id: '',
        total: '',
      }
    },
    watch: {},
    mounted() {
      this.init()
    },
    methods: {
      init() {
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        const parmas = {
          pagesize: this.pagesize,
          page: this.page,
        }
        goodsInList(parmas).then((e) => {
          this.tableData = e.data.data
          this.total = e.data.total
          loading.close()
          if (e.data.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })
      },
      search() {
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        const parmas = {
          pagesize: this.pagesize,
          page: this.page,
          goods_name: this.goods_name,
        }
        goodsInList(parmas).then((e) => {
          this.tableData = e.data.data
          this.total = e.data.total
          loading.close()
        })
      },
      details(e) {
        this.id = e.id
        this.flag = 1
      },
      // 下一页
      handleCurrentChange(val) {
        const _this = this
        _this.page = val
        this.init()
      },
      showMain(e) {
        this.flag = e
        this.init()
      },
    },
  }
</script>

<style></style>
